$(document).ready(function() {
	$("#waiting").hide();
	$("#addcollegefrm").hide();
	$("#modifyform").hide();
	$('#collegelist').datagrid({
		title : "学院管理",
		pagination : false,
		pageSize : 20,
		rownumbers : true,
		displayMsg : "当前第N页",
		idField : "college_name",
		singleSelect : true,
		loadMsg : "正在查询，请稍候。。。。。。",
		toolbar : [{
					text : '添加',
					iconCls : 'icon-add',
					handler : function() {
						showAddDialog();
					}
				}, '-', {
					text : '删除',
					iconCls : 'icon-remove',
					handler : function() {
						var row = $('#collegelist').datagrid('getSelected');
						if (row) {
							$.messager.confirm("确定", "确定要删除" + row.college_name
											+ "吗？", function(r) {
										if (r) {
											$.ajax({
												url : "../course/admin/collegemgr.do",
												type : "POST",
												data : {
													cmd : "del",
													collegename : encodeURIComponent(row.college_name)
												},
												success : function(data,
														textStatus) {
													var result = eval('('
															+ data + ')');
													if (result.error) {
														$.messager.alert("错误",
																result.msg);
													} else {
														$.messager.alert("OK",
																"删除成功");
														window.setTimeout(
																function() {
																	reloadDataGrid(renderPager);
																});
													}
												}
											});
										}
									});
						}
					}
				}, '-', {
					text : '编辑',
					iconCls : 'icon-edit',
					handler : function() {
						var row = $('#collegelist').datagrid('getSelected');
						if (row) {
							$("#waiting").show();
							var options = {
								title : "正在加载" + row.college_name + "的信息",
								closable : false
							};
							$("#waiting").dialog(options);
							$.ajax({
								url : "../course/admin/collegemgr.do",
								type : "POST",
								data : {
									cmd : "getdetail",
									collegename : encodeURIComponent(row.college_name)
								},
								success : function(data, textStatus) {
									$("#waiting").dialog("close");
									var result = eval('(' + data + ')');
									if (result.error) {
										$.messager.alert("错误", result.msg);
									} else {
										$("#collegename")
												.val(result.collegename);
										$("#hidecollege")
												.val(result.collegename);
										$("#desc").val(result.desc);
										if ("1" == result.enable) {
											document.getElementById("enable").checked = true;
										} else {
											document.getElementById("enable").checked = false;
										}
										$("#modifyform").show();
										$("#modifyform").window("open");
									}
								}
							});
						}
					}
				}, '-', {
					text : '专业管理',
					iconCls : 'icon-edit',
					handler : function() {
						var row = $('#collegelist').datagrid('getSelected');
						if (row) {
							if(row.enable=="不可用"){
								$.messager.alert("错误","学院"+row.college_name+"暂不可用");
							}else{
								window.location.href="majormgr.jsp?college="+encodeURIComponent(row.college_name);
							}
						}else{
							$.messager.alert("错误","请选择需要管理专业的学院");
						}
					}
				}]
	});
	reloadDataGrid(renderPager);
});
function renderPager() {
	$.ajax({
				url : "../course/admin/collegemgr.do",
				type : "POST",
				data : {
					cmd : "count"
				},
				success : function(data, textStatus) {
					var result = parseInt(data);
					var options = {
						showPageList : false,
						showRefresh : false,
						pageSize : 40,
						pageNumber : 1,
						total : result,
						displayMsg : "第 {from} 到 {to} ",
						onSelectPage : function(pageNumber, pageSize) {
							$.ajax({
										url : "../course/admin/collegemgr.do",
										type : "POST",
										data : {
											cmd : "listall",
											page : pageNumber
										},
										success : function(data, textStatus) {
											var result = eval('(' + data + ')');
											$('#collegelist').datagrid(
													"loadData", result);
										}
									});
						}
					};
					$("#pager").pagination(options);
				}
			});
}
function addCourseType(courseType) {
	if (courseType == null || courseType.length == 0) {
		$.messager.alert("错误", "请输入学院！");
	}
	$.ajax({
				url : "../course/admin/collegemgr.do",
				type : "POST",
				data : {
					cmd : "add",
					courseclass : encodeURIComponent(courseType)
				},
				success : function(data, textStatus) {
					var result = eval('(' + data + ')');
					if (result.error) {
						$.messager.alert("错误", result.msg);
					} else {
						$.messager.alert("OK", "添加成功");
						window.setTimeout(reloadDataGrid);
					}
				}
			});
}
function hidePager() {
	$("#pager").hide();
}
function showPager() {
	$("#pager").show();
}
function showAddDialog() {
	$("#addcollegefrm").show();
	$("#addcollegefrm").window('open');
}
function closeAddDialog() {
	$("#addcollegefrm").hide();
	$("#addcollegefrm").window('close');
}
function reloadDataGrid(callbackFunc) {
	$.ajax({
				url : "../course/admin/collegemgr.do",
				type : "POST",
				data : {
					cmd : "listall"
				},
				success : function(data, textStatus) {
					var result = eval('(' + data + ')');
					$('#collegelist').datagrid("loadData", result);
					if (callbackFunc) {
						callbackFunc();
					}
				}
			});
}
function showEditDialog(src) {
	$.messager.prompt('修改课程类型', '修改课程类型' + src + '为', function(r) {
				if (typeof(r) == "undefined") {
				} else {
					if (r) {
						modifyCourseType(src, r);
					} else {
						$.messager.alert("错误", "没有输入课程类型");
					}
				}
			});
}
function saveCollege() {
	var src = $("#hidecollege").val();
	var dest = $("#collegename").val();
	var desc = $("#desc").val();
	if (dest.replace(/\s/g, "") == "") {
		$.messager.alert("错误", "学院名不能为空");
		return;
	}
	var options = {
		title : "正在加载保存修改",
		closable : false
	};
	$("#waiting").dialog(options);
	$.ajax({
				url : "../course/admin/collegemgr.do",
				type : "POST",
				data : {
					cmd : "modify",
					src : encodeURIComponent(src),
					dest : encodeURIComponent(dest),
					desc : encodeURIComponent(desc),
					enable : document.getElementById("enable").checked
							? "1"
							: "0"
				},
				success : function(data, textStatus) {
					$("#waiting").dialog("close");
					var result = eval('(' + data + ')');
					if (result.error) {
						$.messager.alert("错误", result.msg);
					} else {
						$("#modifyform").window("close");
						$.messager.alert("OK", "保存成功");
						reloadDataGrid(renderPager);
					}
				}
			});
}
function cancel() {
	$("#modifyform").window("close");
}